package com.ruoyi.web.controller.system;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.vo.TransDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TicketTrans;
import com.ruoyi.system.service.ITicketTransService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 交易信息Controller
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/system/trans")
public class TicketTransController extends BaseController
{
    @Autowired
    private ITicketTransService ticketTransService;

    /**
     * 查询交易信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:trans:list')")
    @GetMapping("/list")
    public TableDataInfo list(TicketTrans ticketTrans)
    {
        startPage();
        List<TicketTrans> list = ticketTransService.selectTicketTransList(ticketTrans);
        return getDataTable(list);
    }

    /**
     * 导出交易信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:trans:export')")
    @Log(title = "交易信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TicketTrans ticketTrans)
    {
        List<TicketTrans> list = ticketTransService.selectTicketTransList(ticketTrans);
        ExcelUtil<TicketTrans> util = new ExcelUtil<TicketTrans>(TicketTrans.class);
        util.exportExcel(response, list, "交易信息数据");
    }

    /**
     * 获取交易信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:trans:query')")
    @GetMapping(value = "/{transNo}")
    public AjaxResult getInfo(@PathVariable("transNo") String transNo)
    {
        return success(ticketTransService.selectTicketTransByTransNo(transNo));
    }

    /**
     * 新增交易信息
     */
    @PreAuthorize("@ss.hasPermi('system:trans:add')")
    @Log(title = "交易信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TicketTrans ticketTrans)
    {
        ticketTrans.setCreateBy(getUsername());
        return toAjax(ticketTransService.insertTicketTrans(ticketTrans));
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TicketTrans> util = new ExcelUtil<TicketTrans>(TicketTrans.class);
        util.importTemplateExcel(response, "交易数据");
    }

    /**
     * 导入交易信息
     */
    @PreAuthorize("@ss.hasPermi('system:trans:import')")
    @Log(title = "交易信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TicketTrans> util = new ExcelUtil<TicketTrans>(TicketTrans.class);
        List<TicketTrans> transList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ticketTransService.importTrans(transList, updateSupport, operName);
        return success(message);
    }


    /**
     * 修改交易信息
     */
    @PreAuthorize("@ss.hasPermi('system:trans:edit')")
    @Log(title = "交易信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TicketTrans ticketTrans)
    {
        ticketTrans.setUpdateBy(getUsername());
        return toAjax(ticketTransService.updateTicketTrans(ticketTrans));
    }

    /**
     * 删除交易信息
     */
    @PreAuthorize("@ss.hasPermi('system:trans:remove')")
    @Log(title = "交易信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{transNos}")
    public AjaxResult remove(@PathVariable String[] transNos)
    {
        return toAjax(ticketTransService.deleteTicketTransByTransNos(transNos));
    }

    @PreAuthorize("@ss.hasPermi('system:trans:add')")
    @Log(title = "根据交易生成收据信息", businessType = BusinessType.INSERT)
    @PostMapping("/generateReceipts")
    public AjaxResult generateReceipts(@RequestBody TransDto transDto)
    {
        String[] transNos = transDto.getTransNos();
        String templateNo = transDto.getTemplateNo();
        String sealNo = transDto.getSealNo();
        return toAjax(ticketTransService.generateReceipts(transNos,templateNo,sealNo));
    }
}
